const app = getApp()

Page({
  data: {
    form: {
      authentication: [''],
      fileCompleted: '',
      inspectionOrganizationName: '',
      supplierQualificationNumber: '',
      qualityTotalEquipmentNumber: '',
      qualityImportedEquipmentNumber: '',
      qualitySelfMadeEquipment: '',
      qualityDomesticNumber: '',
      qualityOtherInformation: ''
    },
    authentication: '',
    fileCompletedList: []
  },
  delFormArray ({ target: { dataset: { key, index } } }) {
    this.data.form[key].splice(index, 1)
    this.setData({
      [`form.${key}`]: this.data.form[key]
    })
  },
  changeAddInput ({ detail: { value }, target: { dataset: { key } } }) {
    this.setData({
      [`${key}`]: value
    })
  },
  addFormArray ({ target: { dataset: { key } } }) {
    this.data.form[key].push('')
    this.setData({
      [`form.${key}`]: this.data.form[key]
    })
  },
  changeCustomInput ({ detail: { data, key } }) {
    this.setData({
      [`form.${key}`]: data
    })
  },
  changeFieldInput ({ detail, target: { dataset: { key } } }) {
    this.setData({
      ['form.' + key]: detail
    })
  },
  changeInput ({ detail: { value }, target: { dataset: { key } } }) {
    this.setData({
      ['form.' + key]: value
    })
  },
  changeRadio ({ detail, target: { dataset: { key } } }) {
    this.setData({
      [`form.${key}`]: JSON.parse(detail)
    })
  },
  preStep () {
    wx.$nav.navigateBack()
  },
  nextStep () {
    let filters = ['fileCompleted', 'inspectionOrganizationName', 'supplierQualificationNumber', 'qualityTotalEquipmentNumber', 'qualityImportedEquipmentNumber', 'qualitySelfMadeEquipment', 'qualityDomesticNumber', 'qualityOtherInformation']
    let valid = filters.every(key => this.data.form[key])
    let valid2 = this.data.form.authentication.length && this.data.form.authentication.every(item => item)
    if (!valid || !valid2) {
      wx.showToast({ title: '请仔细检查是否有未填写的数据！', icon: 'none' })
      return
    }
    app.globalData.form = { ...app.globalData.form, ...this.data.form }
    wx.$nav.navigateTo('/pages/system/index')
  },
  onLoad () {
    const { form, dropDown } = app.globalData
    if (dropDown.fileCompletedList.length) {
      this.setData({
        fileCompletedList: dropDown.fileCompletedList
      })
    } else {
      app.globalData.getDropDownCallback = res => {
        this.setData({
          fileCompletedList: app.globalData.dropDown.fileCompletedList
        })
      }
    }
    let newForm = { ...this.data.form, ...form }
    this.setData({ form: newForm })
    // this.getSelectList()
    // this.getAddres()
  },
})
